package mousemove;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferStrategy;

import chooser.ChooseMenu;
import chooser.GameFrame;

public class GameStart extends GameFrame implements Runnable
{
	private static final long serialVersionUID = 5642619743815256222L;

	private Thread game;
	private boolean isRunning = false;
	private int width, height;
	private BufferStrategy myStrategy;
	
	/**
	 *  Player, only a square currently
	 */
	private Sprite player, robot;

	public GameStart(int i)
	{
		if (i != 0)
			showon();
	}
	
	private void shutdown()
	{
		setIgnoreRepaint(false);
		isRunning = false;
		this.dispose();
		game = null;
		ChooseMenu.getInstance().setVisible(true);
	}
	
	public void showon()
	{
		setUndecorated(true);
		setResizable(false);
		setIgnoreRepaint(true);

		/**
		 * Init FSEM Mode & Create Buffer for Page Flipping
		 */
		GraphicsDevice myDevice;
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		myDevice = ge.getDefaultScreenDevice();
		if (!myDevice.isFullScreenSupported())
		{
			System.out.println("Full-screen exclusive mode not supported");
			shutdown();
		}
		try
		{
			myDevice.setFullScreenWindow(this);
			createBufferStrategy(2);
		}
		catch (Exception e)
		{
			System.out.println("Error while creating buffer strategy");
			shutdown();
		}

		setVisible(true);
		width = getBounds().width;
		height = getBounds().height;
		player = new Player(width, height, 3);
		robot = new Robot(width, height, 2);

		addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e)
			{
				player.setDestination(e.getX(), e.getY());
			}
		});
		addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e)
			{
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
				{
					isRunning = false;
					shutdown();
				}
			}
		});

		startgame();
	}

	public void startgame()
	{
		if (game == null || !isRunning)
		{
			game = new Thread(this);
			game.start();
		}
	}

	public void run()
	{
		isRunning = true;
		// TODO Auto-generated method stub
		while (isRunning)
		{
			gameUpdate();
			myStrategy = getBufferStrategy();
			Graphics graphics = myStrategy.getDrawGraphics();
			try
			{
				graphicUpdate(graphics);
			}
			finally
			{
				graphics.dispose();
			}
			myStrategy.show();
			
			try
			{
				Thread.sleep(30);
			}
			catch (InterruptedException ie)
			{
				
			}
		}
		myStrategy.dispose();
	}

	private void gameUpdate()
	{
		if (isRunning)
		{
			robot.move();
			player.move();
		}
	}

	private void graphicUpdate(Graphics g)
	{
		g.setColor(Color.BLACK);
	    g.fillRect (0, 0, width, height);
	    player.draw(g);
	    robot.draw(g);
	}
}
